package com.lord.otter.component.tools.business.chain;

import com.lord.otter.component.context.spring.StaticSpringContext;

import java.util.Objects;

/**
 * 业务命令启动器
 */
public class BusinessCommandStater {

    private BusinessHandlerManger businessHandlerManger;

    public static BusinessCommandStater create() {
        return new BusinessCommandStater();
    }

    public BusinessCommandStater handler(Class<? extends BusinessHandler> handlerClass) {

        if (Objects.isNull(businessHandlerManger)) {
            businessHandlerManger = BusinessHandlerManger.create();
        }
        BusinessHandler bean = StaticSpringContext.getBeansOfType(handlerClass);
        if (Objects.isNull(bean)) {
            throw new NullPointerException();
        }
        businessHandlerManger.addHandler(bean);
        return this;
    }

    public <C extends BusinessContextAttributeDefinitionTable> BusinessContext<C> start(Object command) {
        return businessHandlerManger.exec(command, null);
    }

    public <C extends BusinessContextAttributeDefinitionTable> BusinessContext<C> start(Object command, BusinessContext<C> context) {
        return businessHandlerManger.exec(command, context);
    }
}
